import React from "react";
import SearchTable from "components/searchTable/SearchTable.jsx";
import { connect } from "dva";
import {
  getFormConfig,
  getTableColumns,
  getAntTableProps,
} from "./tableConfig";

const emptyParams = {
  modelName: '',
  applyUserName: '', // 申请人
  serialNum: '',
  taskName: '',
}
@connect(({
  FlowHandle, loading, appListMenu, Public,
}) => ({
  focusApp: appListMenu.focusApp,
  loading,
  menuDataList: Public.menuDataList,
  processedData: FlowHandle.processedData,
  resourceList: Public.resourceList,
})) export default class ProcessedTaskList extends React.Component {
  state = {
    page: {
      pageNum: 1,
      pageSize: 10,
    },
  }

  searchParams = emptyParams;

  constructor(props) {
    super(props);
    this.getFormConfig = getFormConfig.bind(this);
    this.getTableColumns = getTableColumns.bind(this);
    this.getAntTableProps = getAntTableProps.bind(this);
  }

  componentDidMount() {
    this.initData();
  }

  componentDidUpdate(preProps) {
    const { focusApp } = this.props;
    const { focusApp: preFocusApp } = preProps;
    if (preFocusApp.id && `${preFocusApp.id || 0}` !== `${focusApp.id || 0}`) {
      // 应用切换更新页面数据
      this.resetPageData();
    }
  }

  resetPageData = () => {
    const { dispatch } = this.props;
    this.searchParams = emptyParams;
    if (this.tableForm) {
      this.tableForm.handleReset();
    }
    // dispatch({ type: "FlowHandle/clearData" });
    dispatch({ type: "FlowHandle/set/processedData", payload: { datas: [], totalCount: 0 } });
    this.setState({
        page: { pageNum: 1, pageSize: 10 },
      }, () => {
        this.initData();
      }
    );
  }

  initData = () => {
    const { page } = this.state;
    const { dispatch } = this.props;
    const params = { ...page, ...this.searchParams };
    dispatch({ type:  'FlowHandle/getProProcessedNew', payload: params })
  }

  handleSearch = (values, reset) => {
    const { page } = this.state;
    this.searchParams = reset ? emptyParams : values;
    this.setState({ page: { ...page, pageNum: 1 } }, () => {
      this.initData();
    });
  }

  // 页码页数改变
  handlePageChange = (pageNum, pageSize) => {
    this.setState({ page: { pageNum, pageSize } }, () => {
      this.initData();
    });
  }

  render() {
    return (
      <SearchTable
        ref={r => {
          if (r) {
            this.tableForm = r;
          }
        }}
        formConfig={this.getFormConfig()}
        handleSearch={this.handleSearch}
        tableProps={this.getAntTableProps()}
      />
    );
  }
}

